import './index.less';
export default React.createClass({
    getInitialState(){
        return {
            active: 0
        }
    },
    getDefaultProps(){
        return {
            tabs: [],
            onchange: (index)=> {
            } //变化的index
        }
    },
    componentDidMount(){
        this.setTimer();
    },
    setTimer(){
        const self = this;
        this.timer = setInterval(()=> {
            let now = new Date(), j = -1;
            for (var i = 0; i < self.props.tabs.length; i++) {
                let now_time = new Date().getHours() + ':' + new Date().getMinutes();
                if ((i === 0 && self.props.tabs[i].endtime > now_time && self.props.tabs[i].starttime < now_time) ||
                    (self.props.tabs[i].endtime > now_time && self.props.tabs[i - 1].starttime < now_time)) {
                    j = i;
                    break;
                }
            }
            let _active = self.state.active + 1;
            let current = j > -1 ? j : (_active > self.props.tabs.length - 1 ? 0 : _active);
            console.log(current);
            self.props.onchange(current);
            self.setState({active: current})
        }, 10000);
    },
    clearTimer(){
        this.timer && clearInterval(this.timer);
        this.timer_ && clearTimeout(this.timer_);
    },
    componentWillUnmount(){
        this.clearTimer();
    },
    render(){
        const self = this;
        console.log(this.props.tabs);
        return <ul className="tabs components">
            {
                this.props.tabs.map((item, index)=> {
                    return <li key={index}>
                        <a
                            className={self.state.active == index ? 'active' : ''}
                            href="javascript:void(0)"
                            onClick={()=> {
                                self.setState({active: index});
                                self.clearTimer();
                                self.timer_ = setTimeout(()=> {
                                    //点击后延迟10秒钟返回正常流程
                                    self.setTimer();
                                }, 10000);
                                self.props.onchange(index);
                            }}
                        >
                            {item.subjectname || '暂无'}
                        </a>
                    </li>;
                })
            }
        </ul>
    }
})